import * as Cesium from 'cesium';
function Layer(viewer) {
    this.viewer = viewer;
    this.vectorLayer = null; // 天地图矢量图层
    this.imageryLayer = null; // 天地图影像图层
    this.labelLayer = null; // 天地图矢量标注图层
}

/**
 * @description: 初始化天地图
 * @param {*}
 * @return {*}
 */
Layer.prototype.initLayer = function () {
    const tileMatrixLabels = [
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        '10',
        '11',
        '12',
        '13',
        '14',
        '15',
        '16',
        '17',
        '18'
    ];
    const subdomains = ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'];
    const token = '3b41248c204d6f92ca4fbd7da0386da6';
    let m = parseInt(Math.random() * 8); //0-7随机选择
    // 添加天地图矢量图层
    this.vectorLayer = this.viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
            url:
                `http://t` +
                m +
                `.tianditu.gov.cn/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=` +
                token,
            layer: 'vec',
            crs: 'WGS84', // 指定坐标系为WGS84
            style: 'default',
            format: 'tiles',
            tileMatrixSetID: 'GoogleMapsCompatible',
            subdomains,
            tileMatrixLabels
        })
    );

    // 添加天地图影像图层
    this.imageryLayer = this.viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
            url:
                `http://t` +
                m +
                `.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=` +
                token,
            layer: 'img',
            crs: 'WGS84', // 指定坐标系为WGS84
            style: 'default',
            format: 'tiles',
            tileMatrixSetID: 'GoogleMapsCompatible',
            subdomains,
            tileMatrixLabels
        })
    );

    // 添加天地图矢量标注图层
    this.labelLayer = this.viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
            url:
                `http://t` +
                m +
                `.tianditu.gov.cn/cva_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cva&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=` +
                token,
            layer: 'cva',
            crs: 'WGS84', // 指定坐标系为WGS84
            style: 'default',
            format: 'tiles',
            tileMatrixSetID: 'GoogleMapsCompatible',
            subdomains,
            tileMatrixLabels
        })
    );

    this.vectorLayer.show = false;
};

/**
 * @description: 切换天地图
 * @param {*}
 * @return {*}
 */
Layer.prototype.changeLayer = function (layerType) {
    if (layerType === 'vector') {
        // 显示矢量图层和矢量标注图层，隐藏影像图层
        this.vectorLayer.show = true;
        // this.labelLayer.show = true;
        this.imageryLayer.show = false;
    } else if (layerType === 'image') {
        // 显示影像图层，隐藏矢量图层和矢量标注图层
        this.vectorLayer.show = false;
        // this.labelLayer.show = false;
        this.imageryLayer.show = true;
    }
};

/**
 * @description: 添加geojson面数据
 * @param {*}
 * @return {*}
 */
Layer.prototype.initGeoJsonLayer = function (data) {
    const self = this;
    const styleObj = data.style || {};
    const geojsonData = data.geojson;
    // 加载GeoJSON数据到Cesium
    var dataSource = new Cesium.GeoJsonDataSource();
    dataSource
        .load(geojsonData, {
            fill: styleObj.fill || Cesium.Color.fromCssColorString('rgba(118, 218, 72, 0)'),
            stroke: styleObj.stroke || Cesium.Color.fromCssColorString('#569cd6'),
            strokeWidth: styleObj.strokeWidth || 0
        })
        .then(function () {
            self.viewer.dataSources.add(dataSource);
        })
        .catch(function (error) {
            console.error('加载GeoJSON数据失败：', error);
        });

    return dataSource;
};

export default Layer;
